package com.deft.design.template.group;

import com.alibaba.fastjson.JSON;
import com.deft.design.template.HttpClient;
import com.deft.design.template.NetMall;
import sun.misc.BASE64Encoder;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author Hide丶love
 * @date 2022/05/30
 **/
public class JdNetMall extends NetMall {

    Pattern pattern = Pattern.compile("(?<=title>).*(?=</title)");

    public JdNetMall(String uId, String uPwd) {
        super(uId, uPwd);
    }

    @Override
    protected Boolean login(String uId, String uPwd) {
        logger.info("模拟京东用户登录 uId：{} uPwd：{}", uId, uPwd);
        return true;
    }

    @Override
    protected Map<String, String> reptile(String skuUrl) {
        String str = HttpClient.doGet(skuUrl);
        Matcher matcher = pattern.matcher(str);
        Map<String, String> map = new ConcurrentHashMap<>();
        if (matcher.find()) {
            map.put("name", matcher.group());
        }
        map.put("price", " 879.00");
        logger.info("模拟京东商品爬虫解析：{}  |  {} 元 {}", map.get("name"), map.get("price"), skuUrl);
        return map;
    }

    @Override
    protected String createBase64(Map<String, String> map) {
        BASE64Encoder base64Encoder = new BASE64Encoder();
        logger.info("模拟生成京东商品base64海报");
        return base64Encoder.encode(JSON.toJSONString(map).getBytes());
    }
}
